/**
 * 登录js
 * Created by mhy on 2017/12/9.
 */
var count = 0;      //记录登录次数
$(document).ready(function() {
    /* 点击提交 */
    $("#submit").click(function () {
        //获取到相应的值
        var $username = $("input[name='username']").val();      //用户名
        var $password = $("input[name='password']").val();      //密码
        var $checkCode = $("input[name='checkCode']").val();    //验证码
        var $types = $("input[type='radio']");                  //类型按钮
        var type;                          //选中的类型 如何管理员、部门等
        var $this = $(this);    //#submit 对象
        var action;

        $this.css("color","lightsteelblue");
        $this.text("登录中");
        $this.attr("disabled","disabled");

        /* 获得选中的类型的值 */
        for(var i = 0;i < $types.length;i++) {
            if ($types.eq(i).is(":checked")) {
                type = parseInt($types.eq(i).val());
                break;
            }
        }


        if($username.length == 0 || $password.length == 0 || type.length == 0){
            var err_msg = "请输入用户名和密码";
            createPNotify("提示信息","info",err_msg);

            $this.css("color","#FFF");
            $this.text("登录");
            $this.removeAttr("disabled");
            return;
        }else if($checkCode.length == 0){
            createPNotify("提示信息","info","请输入验证码");

            $this.css("color","#FFF");
            $this.text("登录");
            $this.removeAttr("disabled");
            return;
        }

        /*设置表单提交地址*/
        action = $("form").attr("action");
        if(type == 0)
            $("form").attr("action",action + "staff/login");
        else
            $("form").attr("action",action + "admin/login");

        $("form").submit();     //提交表单
    });
    changeImg();
});

$(document).keydown(function(event){
    enterLogin(event);
});

/*实现倒计时*/
function time(time_seconds,obj) {
    if (time_seconds == 0) {
        $(obj).removeAttr("disabled");
        $(obj).text("登录");

        $(document).keydown(function(event){
            enterLogin(event);
        });

    } else {
        time_seconds--;
        obj.text("剩余:" + time_seconds + "秒");
        setTimeout(function() {
                time(time_seconds,obj)
            },1000)
    }
}

/*函数:回车登录*/
function enterLogin(event) {
    if(event.keyCode == 13){    //按下回车
        $("#submit").trigger("click");
    }
}

